import {
  BaseRepository,
  indexPaginateBuilder,
  showBuilderEntity,
} from '@libs/common/repository';
import { Inject, Injectable } from '@nestjs/common';
import { Repository } from 'typeorm';

import { IndexSysUserDto, ShowSysUserDto } from './dto/sys-user.dto';
import { SysUser } from './entities/sys-user.entity';
import { ProviderKey } from './sys-user.providers';

@Injectable()
export class SysUserService extends BaseRepository {
  @Inject(ProviderKey)
  private repository: Repository<SysUser>;

  async index(dto: IndexSysUserDto) {
    const res = await indexPaginateBuilder({
      dto,
      repository: this.repository,
    });
    return res;
  }

  show<D extends ShowSysUserDto = ShowSysUserDto>(dto: D) {
    return showBuilderEntity(this.repository, dto);
  }

  async userInfo(id: string) {
    const userInfo = await this.show({ id });

    if (!userInfo) throw new Error('用户不存在');
    return {
      id,
      uid: id,
      tokenid: '',
      type: 1,
      username: userInfo?.username,
      nickname: userInfo?.nickname,
    };
  }
}
